package com.olddriver.crm.web.controller;

import com.olddriver.crm.domain.Client;
import com.olddriver.crm.page.PageResult;
import com.olddriver.crm.query.ClientQueryObject;
import com.olddriver.crm.service.IClientService;
import com.olddriver.crm.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("client")
public class ClientController {
    @Autowired
    private IClientService clientService;

    //跳转到潜在客户的页面
    @RequestMapping("fake_view")
    public String fake_view() {
        return "client/client_fake";
    }

    //跳转到正式客户的页面
    @RequestMapping("formal_view")
    public String formal_view() {
        return "client/client_fomal";
    }

    //跳转到资源池客户的页面
    @RequestMapping("pool_view")
    public String pool_view() {
        return "client/client_pool";
    }

    //跳转到资源池客户的页面
    @RequestMapping("moveAll_view")
    public String moveAll_view() {
        return "client/client_moveAll";
    }

    //查询不同状态的客户
    @ResponseBody
    @RequestMapping("selectByStatus")
    public PageResult query(ClientQueryObject qo) {
        PageResult result = clientService.query(qo);
        return result;
    }

    @ResponseBody
    @RequestMapping("saveOrUpdate")
    public JsonResult saveOrUpdate(Client client) {
        try {
            if (client.getId() != null) {
                clientService.update(client);
            } else {
                clientService.save(client);
            }
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.faliure();
        }
    }

    //客户的移交
    @ResponseBody
    @RequestMapping("updateTakeUser")
    public JsonResult updateTakeUser(Client client,String reason) {
        try {
            clientService.updateTakeUser(client,reason);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.faliure();
        }
    }

    //改变客户的状态,放入资源池
    @RequestMapping("updateStatus")
    @ResponseBody
    public JsonResult updateStatus(Long id,Byte status,String reason) throws Exception{
        try {
            clientService.updateStatus(id,status,reason);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.faliure();
        }
    }

    @ResponseBody
    @RequestMapping("selectNoTakeUser")
    public Object selectNoTakeUser(ClientQueryObject qo) {
        List<Client> clients = clientService.selectNoTakeUser(qo);
        return clients;
    }
}
